package com.netty8018.echo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * @author liyanhao
 * @Date 2023-02-03 11:13
 */
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    //读取到网络数据进行后台业务处理
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        System.out.println("client accept:" + msg.toString(CharsetUtil.UTF_8));
        ctx.close();
    }
    //channel 活跃后，做业务处理
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("Hello,Netty", CharsetUtil.UTF_8));
    }
}
